package com.llgz.hotelmanageapi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llgz.hotelmanageapi.common.CommonResult;
import com.llgz.hotelmanageapi.pojo.DO.CommodityCategory;
import com.llgz.hotelmanageapi.service.impl.CommodityCategoryServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品类别信息 前端控制器
 * </p>
 *
 * @author Bernie_fang
 * @since 2021-07-19
 */
@RestController
@RequestMapping("/commodityCategory")
@Api("商品类别信息")
@RequiresRoles(value = {"root", "admin", "manager", "reception"}, logical = Logical.OR)
public class CommodityCategoryController {
    @Autowired
    CommodityCategoryServiceImpl commodityCategoryService;

    /**
     * @MethodName addCommodityCategory
     * @Params * @param CommodityCategory
     * @Description 添加
     * @Return CommonResult
     */
    @PostMapping("/addCommodityCategory")
    @ApiOperation(value = "添加", response = CommonResult.class, httpMethod = "POST")
    public CommonResult addCommodityCategory(@RequestBody CommodityCategory commodityCategory){
        try {
            boolean save = commodityCategoryService.save(commodityCategory);
            return CommonResult.successResponse(save);
        }catch (Exception e){
            return CommonResult.errorResponse("类别已存在");
        }
    }
    /**
     * @MethodName getCommodityCategoryList
     * @Params * @param CommodityCategory
     * @Description 查找全部
     * @Return CommonResult
     */
    @GetMapping("/getCommodityCategoryList")
    @ApiOperation(value = "查找", response = CommonResult.class, httpMethod = "GET")
    public CommonResult getCommodityCategoryList(@RequestParam(required = false) Integer typeCode,@RequestParam String typeName,@RequestParam Integer pageNum,@RequestParam Integer pageCount){
        QueryWrapper<CommodityCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("type_code");
        if (typeCode != null){
            queryWrapper.like("type_code",typeCode);
        }
        if (typeName != null && !typeName.isEmpty()){
            queryWrapper.like("type_name",typeName);
        }
        IPage<CommodityCategory> page = commodityCategoryService.page(new Page<>(pageNum, pageCount), queryWrapper);
        return CommonResult.successResponse(page);
    }
//    @GetMapping("/getCommodityCategoryByCode")
//    public CommonResult getCommodityCategoryByCode(@RequestParam(required = false) Integer typeCode){
//        CommodityCategory category = commodityCategoryService.getOne(new QueryWrapper<CommodityCategory>().eq("type_code", typeCode));
//        return CommonResult.successResponse(category);
//    }
    @GetMapping("/getCommodityCategoryById")
    public CommonResult getCommodityCategoryById(@RequestParam String id){
        CommodityCategory category = commodityCategoryService.getOne(new QueryWrapper<CommodityCategory>().eq("id", id));
        return CommonResult.successResponse(category);
    }

    /**
     * @MethodName updateCommodityCategory
     * @Params * @param CommodityCategory
     * @Description 更新
     * @Return CommonResult
     */
    @PostMapping("/updateCommodityCategory")
    @ApiOperation(value = "更新", response = CommonResult.class, httpMethod = "POST")
    public CommonResult updateCommodityCategory (@RequestBody CommodityCategory commodityCategory){
        boolean b = commodityCategoryService.updateById(commodityCategory);
        return CommonResult.successResponse(b);
    }
//    /**
//     * @MethodName deleteCommodityCategory
//     * @Params * @param CommodityCategory
//     * @Description 删除
//     * @Return CommonResult
//     */
//    @PostMapping("/deleteCommodityCategory")
//    @ApiOperation(value = "删除", response = CommonResult.class, httpMethod = "POST")
//    public CommonResult deleteCommodityCategory (@RequestParam String id){
//        boolean b = commodityCategoryService.removeById(id);
//        return CommonResult.successResponse(b);
//    }
    /**
     * @MethodName deleteCommodityCategory
     * @Params * @param CommodityCategory
     * @Description 删除（多选）
     * @Return CommonResult
     */
    @PostMapping("/deleteCommodityCategory")
    @ApiOperation(value = "删除（多选）", response = CommonResult.class, httpMethod = "POST")
    public CommonResult deleteCommodityCategory (@RequestBody List<String> ids){
        boolean b = commodityCategoryService.removeByIds(ids);
        return CommonResult.successResponse(b);
    }
}

